﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_popups
{
    public partial class Addlid : Form
    {
        string naam;
        string taak;
        int projectID;

        public Addlid()
        {

        }

        public Addlid(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getTaken();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            naam = comboBox1.SelectedItem.ToString(); // moet de lijst met leden worden die in het project zitten, + lidID is nodig

            taak = comboBox2.SelectedItem.ToString(); // lijst met taken, taakID moet meegegeven worden

            if (naam != "" && taak != "")
            {
                addMember();
            }
            else
            {
                MessageBox.Show("Niet alle velden zijn ingevuld!");
            }
        }

        public void checkId()
        { 
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand takenGet = new MySqlCommand("SELECT TaakID FROM taken WHERE Taak='"+naam+"'", grabConnection);
               
                grabConnection.Open();
                
                MySqlDataReader reader = takenGet.ExecuteReader();
                
                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        takenGet.Dispose();
                    }
                    else
                    {
                        while (reader.Read())
                        {
                            var taaknr = reader["TaakID"];
                        }

                        reader.Close();
                    }

                    grabConnection.Close();
                    reader.Dispose();
                    takenGet.Dispose();
                }
            }

            catch (MySqlException)
            {
                MessageBox.Show("Sql error detected!");
            }
        }
        
        public void addMember()
        {
            checkId();
            MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
            MySqlCommand taken = new MySqlCommand("INSERT INTO taak_lid (TaakID, UserID )" + "VALUES (1,1)", grabConnection);
 
            try
            {
                grabConnection.Open();
                taken.ExecuteNonQuery();
            }
            catch (Exception)
            {
                MessageBox.Show("Sql error detected!");
            }
            finally
            {
                grabConnection.Close();
                MessageBox.Show("Taak succesvol opgeslagen!");
                this.Close();
            }
        }

        public void getTaken()
        {
            try
            {
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand takenGet = new MySqlCommand("SELECT TaakID, Taak, Status FROM taken WHERE ProjectID='" + projectID + "'", grabConnection);

                //takenGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                grabConnection.Open();

                MySqlDataReader reader = takenGet.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        takenGet.Dispose();
                    }
                    else
                    {
                        comboBox2.Items.Clear();

                        while (reader.Read())
                        {
                            comboBox2.Items.Add(reader["Taak"].ToString());
                            //comboBox2.Items.Add(reader["TaakID"]);
                        }

                        reader.Close();
                    }

                    grabConnection.Close();
                    reader.Dispose();
                    takenGet.Dispose();
                }
            }

            catch (MySqlException)
            {
                MessageBox.Show("Sql error detected!");
            }
        }
    }
}
